// Copyright Rachael Colley 2013.

package rach.grid.andgrid1;

import java.util.Map;


public class WorldGrid {
	
	private Map<Integer,Map<Integer,Map<Integer,GridCell>>> zMap;
	private WorldParameters wp;
	
	public WorldGrid(WorldParameters wp) {
		this.wp = wp;
	}

	public Map<Integer, Map<Integer, Map<Integer, GridCell>>> getzMap() {
		return zMap;
	}

	public void setzMap(Map<Integer, Map<Integer, Map<Integer, GridCell>>> zMap) {
		this.zMap = zMap;
	}
	
	public int getNumberOfZEntries() {
		return zMap.size();
	}
	
	public String getTotalNumberOfEntries() {
		int countZ = 0;
		int countY = 0;
		int countX = 0;
		
		for (Map.Entry<Integer, Map<Integer,Map<Integer,GridCell>>> zEntry : zMap.entrySet()) {
			countZ ++;
			int zKey = zEntry.getKey();
			Map<Integer,Map<Integer,GridCell>> yMap = zEntry.getValue();
			
			for (Map.Entry<Integer,Map<Integer,GridCell>> yEntry : yMap.entrySet()) {
				countY++;
				int yKey = yEntry.getKey();
				Map<Integer,GridCell> xMap = yEntry.getValue();
				
				for (Map.Entry<Integer,GridCell> xEntry : xMap.entrySet()) {
					countX++;
					int xKey = xEntry.getKey();
					GridCell gc = xEntry.getValue();
				}
				
			}
			
		}
		
		return "z: " + countZ + " y: " + countY + " x: " + countX;
	}
	
	public int getNumberOfYEntriesForLevel(int level) {
		Map<Integer,Map<Integer,GridCell>> yMap = zMap.get(level);
		return yMap.size();
	}
	
	public int getNumberOfXEntriesForY(int level, int yCoordinate) {
		Map<Integer,Map<Integer,GridCell>> yMap = zMap.get(level);
		Map<Integer,GridCell> xMap = yMap.get(yCoordinate);
		return xMap.size();
	}
	
	public int getNumberOfPopulatedGridCellsForLevelForWorld() {
		int count = 0;
		for (Map.Entry<Integer,Map<Integer,Map<Integer,GridCell>>> zEntry : zMap.entrySet()) {
			int zKey = zEntry.getKey();
			count += getNumberOfPopulatedGridCellsForLevel(zKey);
		}
		return count;
	}
	
	public int getNumberOfPopulatedGridCellsForLevel(int level) {
		int count = 0;
		Map<Integer,Map<Integer,GridCell>> yMap = zMap.get(level);
		for (Map.Entry<Integer,Map<Integer,GridCell>> yEntry : yMap.entrySet()) {
			int yKey = yEntry.getKey();
			count += getNumberOfPopulatedGridCellsForY(level,yKey);
		}
		return count;
	}
	
	public int getNumberOfPopulatedGridCellsForY(int level, int yCoordinate) {
		int count = 0;
		Map<Integer,GridCell> xMap = zMap.get(level).get(yCoordinate);
		for (Map.Entry<Integer, GridCell> xEntry : xMap.entrySet()) {
			int xKey = xEntry.getKey();
			GridCell gc = xEntry.getValue();
			if (gc != null) {
				count ++;
			}
		}
		return count;
	}
	
	

}
